﻿using BrightIdeasSoftware;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rename.UC.FastOLSV
{
    internal class MyRearrangingDropSink:SimpleDropSink
    {
        public override void DrawFeedback(Graphics g, Rectangle bounds)
        {
            // 可选：自定义拖拽时的反馈效果
            base.DrawFeedback(g, bounds);
        }
        protected override void OnModelCanDrop(ModelDropEventArgs args)
        {
            base.OnModelCanDrop(args);
        }

        protected override void OnModelDropped(ModelDropEventArgs args)
        {
            // 这是关键方法 - 当拖拽完成时调用
            if (args.SourceListView == null || args.TargetModel == null)
                return;

            var sourceList = args.SourceListView.Objects as IList;
            var targetList = this.ListView.Objects as IList;

            if (sourceList == null || targetList == null || sourceList != targetList)
                return;

            // 获取要移动的对象
            object modelToMove = args.SourceModels;

            // 获取目标位置的对象
            object targetModel = args.TargetModel;

            // 获取目标位置在列表中的索引
            int targetIndex = targetList.IndexOf(targetModel);
            if (targetIndex == -1) return;

            // 先移除再插入
            targetList.Remove(modelToMove);

            // 调整插入位置（如果是从后面拖到前面）
            int currentIndex = targetList.IndexOf(targetModel);
            if (currentIndex != -1)
            {
                targetList.Insert(currentIndex, modelToMove);
            }
            else
            {
                targetList.Add(modelToMove);
            }

            // 刷新列表显示
            this.ListView.BuildList();
        }

        //public override void ModelCanDrop(ModelDropEventArgs args)
        //{
        //    base.ModelCanDrop(args);

        //    // 在这里可以添加自定义的验证逻辑
        //    // 例如：args.Effect = DragDropEffects.Move;
        //}
    }
}
